package com.lam.communication;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ToggleButton;

import com.lam.fundamental.R;

public class MailActivity extends Activity {

	private Button bt_send;
	private String [] strEmailReciver;
	private String subject;
	private String mailCC;
	private String mialContent;
	private EditText et_reciver;
	private EditText et_subject;
	private EditText et_mailCC;
	private EditText et_content;
	private Vibrator vibrator;
	private ToggleButton tb_vibrator;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.comunication_email);
		bt_send = (Button)findViewById(R.id.bt_mailSend);
		et_content=(EditText)findViewById(R.id.et_mailContent);
		et_mailCC=(EditText)findViewById(R.id.et_mailcc);
		et_reciver=(EditText)findViewById(R.id.et_reciever);
		et_subject=(EditText)findViewById(R.id.et_subject);
		tb_vibrator=(ToggleButton)findViewById(R.id.tb_vibratior);
		vibrator=(Vibrator)getApplication().getSystemService(Service.VIBRATOR_SERVICE);
		tb_vibrator.setOnCheckedChangeListener(new OnCheckedChangeListener() {
		
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked){
					vibrator.vibrate(new long[] {2000,100,2000,1000},1);
				}else{
					vibrator.cancel();
				}
			}
		});
//		et_reciver.setOnKeyListener(new OnKeyListener() {
//			
//			@Override
//			public boolean onKey(View v, int keyCode, KeyEvent event) {
//				// TODO Auto-generated method stub
//				if(isEmail(et_reciver.getText().toString())){
//					bt_send.setEnabled(true);
//				}else{
//					bt_send.setEnabled(false);
//				}
//				return false;
//			}
//			
//		});
		
		bt_send.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent emailIntent=new Intent(android.content.Intent.ACTION_SEND);
				emailIntent.setType("plain/text");
				strEmailReciver=new String[]{et_reciver.getText().toString()};
				mailCC=et_mailCC.getText().toString();
				subject=et_subject.getText().toString();
				mialContent=et_content.getText().toString();
				emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, strEmailReciver);
				emailIntent.putExtra(android.content.Intent.EXTRA_CC, mailCC);
				emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
				emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, mialContent);
				
				startActivity(Intent.createChooser(emailIntent, "Chose a way!"));
			}
		});
	}
	
	public boolean isEmail(String string) {
		// TODO Auto-generated method stub
		String pattern="^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-][a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
		Log.i(string,string);
		Pattern p=Pattern.compile(pattern);
		Matcher m=p.matcher(pattern);
		Log.i("test",m.matches()+"");
		return m.matches();
	}
	
}
